use std::str;
use std::str::Utf8Error;

fn _main1() {
    println!("Hello, world!");
    let pangram: &'static str = "the quick brown fox jomps over the lazy dog";
    println!("Pangram: {}", pangram);

    println!("Words in reverse");
    for word in pangram.split_whitespace().rev() {
        println!("> {}", word);
    }

    let mut chars: Vec<char> = pangram.chars().collect();
    chars.sort();
    chars.dedup();

    let mut string = String::new();
    for c in chars {
        string.push(c);
        string.push_str(", ")
    }

    let chars_to_trim: &[char] = &[' ', ','];

    let trimmed_str: &str = string.trim_matches(chars_to_trim);
    println!("Used characters:{}", trimmed_str);
    let alice = String::from("I like dogs");
    let bob: String = alice.replace("dog", "cat");

    println!("Alice says {}", alice);
    println!("Bob says : {}", bob);
}

fn _main2() {
    let byte_escape = "I am writing \x52\x75\x73\x74";
    println!("What are you doing \x3F(\\3F means ?) {}", byte_escape);

    let unicode_codepoint = "\u{211D}";
    let character_name = "\"DOUBLE-STRUCK CAPITAL R\"";
    println!(
        "Unicode character {}(u+211D) is called {}",
        unicode_codepoint, character_name
    );

    let long_string =
        "String literals can span multiple lines . the line break and indentation here -> \
    <- can be escaped too！";
    println!("{}", long_string);
}

fn _main() {
    let raw_str = r"Escapes don't work here: \x3F \u{211D}";
    println!("{}", raw_str);

    let quotes = r#"And then i said: "There is no escape!""#;
    println!("{}", quotes);

    let longer_delimiter = r####"A string with "# in it and even "##!"####;
    println!("{}", longer_delimiter);
}

fn main() {
    let bytestring: &[u8; 20] = b"this is a bytestring";

    println!("A bytestring: {:?}", bytestring);

    let escaped = b"\x52\x75\x73\x74 as bytes";

    println!("Some escaped bytes: {:?}", escaped);

    let raw_bytestring = br"\u{211D} is not escaped here";
    println!("{:?}", raw_bytestring);

    if let Ok(my_str) = str::from_utf8(raw_bytestring) {
        println!("And the same as text: {}", my_str);
    }

    let quotes = br#"You can also use "fancier" formatting, \
    like with normal raw strings "#;
    let shift_jis = b"\x82\xe6\x82\xa8\x82\xb1\x82"; // SHIFT-JIS 编码的 "ようこそ"

    // 但这样的话它们就无法转换成 &str 了
    match str::from_utf8(shift_jis) {
        Ok(my_str) => println!("Conversion successful: '{}'", my_str),
        Err(e) => println!("Conversion failed: {:?}", e),
    };
}
